using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

public class ECSManager : MonoBehaviour
{
    public int entityCount = 100;
    public float spawnRadius = 10f;
    void Start()
    {
        var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        // 创建实体原型（包含所需组件）
        var archetype = entityManager.CreateArchetype(
            typeof(MoveDataTag),
            typeof(SpeedTag),
            typeof(LocalTransform)
        );

        // 批量创建实体
        using (var entities = new NativeArray<Entity>(entityCount, Allocator.Temp))
        {
            entityManager.CreateEntity(archetype, entities);

            //    for (int i = 0; i < entityCount; i++)
            //    {
            //        // 设置随机位置
            //        float3 randomPos = new float3(
            //            UnityEngine.Random.Range(-spawnRadius, spawnRadius),
            //            UnityEngine.Random.Range(-spawnRadius, spawnRadius),
            //            UnityEngine.Random.Range(-spawnRadius, spawnRadius)
            //        );

            //        entityManager.SetComponentData(entities[i], new MoveDataTag(randomPos));
            //        entityManager.SetComponentData(entities[i], new DistanceComponent(0f));

            //        // 可选的配置组件
            //        var config = DistanceConfigComponent.Default;
            //        config.UseSquaredDistance = true; // 使用平方距离优化性能
            //        entityManager.SetComponentData(entities[i], config);
            //    }
        }

        Debug.Log($"生成了 {entityCount} 个测试实体");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
